//***************************
//
// 画表格 辅助工具
var tableHelper = (function () {
    var drawPanel = function (option) {
        return $("<div class='myCreateTable_panel panel panel-default'>").append(
            option.title ? $("<div class='panel-heading'>").append(option.title) : '',
            $("<div class='panel-body'>")
        );
    };
    var initTable = function (option) {
        var heads = [];
        for (var i in option.colNames) {
            heads.push(
                $("<th>").text(option.colNames[i])
            );
        }
        return $("<table class='table myCreateTable display' style='width: 100%'>").append(
            $("<thead>").append(
                $("<tr>").append(heads)
            )
        )
    };
    /**
     * title / colNames / dataTableCfg{ajax,columns}
     * @param targetSelector
     * @param optionIn
     */
    var drawTable = function (targetSelector, optionIn) {
        var option = $.extend(true, {
            title: '', //标题 可以插入元素
            colNames: ["选择"], //列头的名字
            dataTableCfg: { //表格的参数
                "searching": false,
                select: {
                    items: 'row',
                    style: 'os',
                    selector: 'td'
                },
                // "dom": 'lfrtip',
                "ajax": {
                    "url": "",
                    "contentType": "application/json",
                    "type": "POST",
                    "data": function (d) {
                        return JSON.stringify(d);
                    }
                },
                "columns": [{
                    orderable: false,
                    className: 'select-checkbox',
                    data: null,
                    "defaultContent": ""
                }],
                "language": language,
                "initComplete": function (settings, json) {
                    $(".myCreateTable").find('input[type="search"]').addClass("form-control")
                }
            }
        }, optionIn);

        var target = $(targetSelector).empty();
        var table = initTable(option);
        var panel = drawPanel(option);
        panel.find(".panel-body").append(table);
        target.append(panel);
        var res = table.DataTable(option.dataTableCfg);

        return res;
    };

    /**
     * 生成一个列表项
     * {
            "data": null,
            "defaultContent": "",
            "render": function (data, type, row) {
                return '';
            }
        }
     * @param optionIn
     */
    var genCol = function (optionIn) {
        return $.extend(true, {
            "data": null,
            "defaultContent": ""
        }, optionIn);
    };

    var genCol_name = function (name) {
        return genCol({data: name})
    };
    /**
     * "render": function (data, type, row) {
     * @param func
     */
    var genCol_rander = function (func) {
        return genCol({render: func})
    };

    /**
     *
     * @param name
     * @param func 参数 data, type, row
     */
    var genCol_mix = function (name, func) {
        return genCol({
            data: name,
            render: func
        })
    };

    var language = {
        "sProcessing": "处理中...",
        "sLengthMenu": "显示 _MENU_ 项结果",
        "sZeroRecords": "没有匹配结果",
        "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
        "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
        "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
        "sInfoPostFix": "",
        "sSearch": "搜索:",
        "sUrl": "",
        "sEmptyTable": "表中数据为空",
        "sLoadingRecords": "载入中...",
        "sInfoThousands": ",",
        "oPaginate": {
            "sFirst": "首页",
            "sPrevious": "上页",
            "sNext": "下页",
            "sLast": "末页"
        },
        "oAria": {
            "sSortAscending": ": 以升序排列此列",
            "sSortDescending": ": 以降序排列此列"
        },
        "select": {
            "rows": {
                _: "选择了 %d 条记录",
                1: "选择了 1 条记录"
            }
        }
    };

    /**
     *弹出提示框
     * @param msg 消息
     * @param isgood  1/0 -> success / error
     */
    var showMsg = function (msg, isgood) {
        Messenger().post({
            message: msg,
            type: isgood ? "success" : 'error',
            showCloseButton: true
        });
    };
    return {
        drawTable: drawTable,
        genCol: genCol,
        genCol_name: genCol_name,
        genCol_rander: genCol_rander,
        genCol_mix: genCol_mix,
        showMsg: showMsg,
    }
})();
